package com.ross.web.maintenance;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.velocity.components.SetDirective;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.dao.UsersDAO;
import com.ross.web.domain.Users;
import com.ross.web.util.DateUtil;
import com.ross.web.util.Encrypter;
import com.ross.web.util.SessionLogout;

public class UserMaintenanceAction extends ActionSupport {
	
	private List<Users> userList;
	private int dispMode;
	private Users users;
	private String searchQuery;
	private String confirmpassword;
	
	UsersDAO usersDAO = new UsersDAO();
	
	private Encrypter e = new Encrypter();
	private SessionLogout sessionLogout = new SessionLogout();
	private HttpServletRequest request = ServletActionContext.getRequest();
	
	public String goToUserMaintenanceAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		userList = usersDAO.getAllUsers();
		
		setDispMode(1);
		return SUCCESS;
	}
	
	public String goToAddUserAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		setDispMode(2);
		return SUCCESS;
	}
	
	public String doAddUserAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		if(!validateInputs()){
			setDispMode(2);
			return ERROR;
		}else{
			insert();
		}
		
		userList = usersDAO.getAllUsers();
		setDispMode(1);
		return SUCCESS;
	}
	
	public String goToUpdateUserAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		users = usersDAO.getUsersById(users.getUserid());
		
		setDispMode(3);
		return SUCCESS;
	}
	
	public String doUpdateUserAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		if(!validateInputs()){
			setDispMode(3);
			return ERROR;
		}else{
			update();
		}
		
		userList = usersDAO.getAllUsers();
		setDispMode(1);
		return SUCCESS;
	}
	
	public String deleteUserAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try{
			
			usersDAO.delete(users);
			
			addActionMessage(getText("success.user.delete"));
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		setDispMode(1);
		userList = usersDAO.getAllUsers();
		return SUCCESS;
	}
	
	public String serachUserAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		
		userList = usersDAO.getUsersByUsername(searchQuery);
		setDispMode(1);
		return SUCCESS;
		
	}
	
	private boolean validateInputs(){
		
		if(users.getUsername().trim().length() == 0){
			addActionError(getText("error.user.usernamerequired"));
		}else if(users.getUsername().length() >= 8){
			addActionError(getText("error.user.usernameexceed"));
		}else if(dispMode == 2){
			
			if(usersDAO.getUsersByUsernameExact(users.getUsername()) != null ){
				addActionError(getText("error.user.usernameexist"));
			}
		}
		
		if(users.getFirstname().trim().length() == 0){
			addActionError(getText("error.user.firstnamerequired"));
		}else if(users.getFirstname().length() >=21){
			addActionError(getText("error.users.firstnameexceed"));
		}
		
		if(users.getMiddlename().trim().length() == 0){
			addActionError(getText("error.user.middlenamerequired"));
		}else if(users.getMiddlename().length() >= 21){
			addActionError(getText("error.user.middlenameexceed"));
		}
		
		if(users.getLastname().trim().length()== 0){
			addActionError(getText("error.user.lastname"));
		}else if(users.getLastname().length() >= 21){
			addActionError(getText("error.user.lastnameexceed"));
		}
		
		if(users.getAddress().trim().length() == 0){
			addActionError(getText("error.user.addressrequired"));
		}else if(users.getAddress().length() >= 150){
			addActionError(getText("error.user.addressexceed"));
		}
		
		if(users.getTelephone().trim().length() == 0){
			addActionError(getText("error.user.telephonerequired"));
		}else if(users.getTelephone().length() >= 11){
			addActionError(getText("error.user.telephoneexceed"));
		}
		
		if(users.getCellphone().trim().length() == 0){
			addActionError(getText("error.user.cellphonerequired"));
		}else if(users.getCellphone().length() >= 16){
			addActionError(getText("error.user.cellphoneexceed"));
		}
		
		if(users.getEmail().trim().length() == 0){
			addActionError(getText("error.user.emailrequired"));
		}else if(users.getEmail().length() >= 101){
			addActionError(getText("error.user.emailexceed"));
		}
		
		System.out.println("rossdispmode="+dispMode);
		if(dispMode == 3){
		
			if(users.getPassword().trim().length() == 0){
				addActionError(getText("error.user.passwordrequired"));
			}
			
			if(confirmpassword.trim().length() == 0){
				addActionError(getText("error.user.confirmpasswordrequired"));
			}else{
				
				if(!users.getPassword().equals(confirmpassword)){
					addActionError(getText("error.user.passwordnotmatch"));
				}
			}
		}
		
		if(this.hasActionErrors()){
			return false;
		}else{
			return true;
		}
	}
	
	private void insert(){
		try{
			users.setPassword(e.encrypt("password"));
			users.setDatecreated(DateUtil.getDateToday());
			users.setStatus("active");
			usersDAO.insert(users);
			addActionMessage(getText("success.users.add"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void update(){
		try{
			users.setPassword(e.encrypt(users.getPassword()));
			usersDAO.update(users);
			
			addActionMessage(getText("success.users.update"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	public String getConfirmpassword() {
		return confirmpassword;
	}

	public void setConfirmpassword(String confirmpassword) {
		this.confirmpassword = confirmpassword;
	}

	public String getSearchQuery() {
		return searchQuery;
	}

	public void setSearchQuery(String searchQuery) {
		this.searchQuery = searchQuery;
	}

	public Users getUsers() {
		return users;
	}
	
	public void setUsers(Users users) {
		this.users = users;
	}

	public List<Users> getUserList() {
		return userList;
	}

	public void setUserList(List<Users> userList) {
		this.userList = userList;
	}

	public int getDispMode() {
		return dispMode;
	}

	public void setDispMode(int dispMode) {
		this.dispMode = dispMode;
	}
	
	
}
